package com.liuyk.util;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.BounceInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.RelativeLayout;

import com.liuyk.activity.WarpView;

/**
 * 属性动画
 * <p>
 * Created by Liuyk on 2015/6/26.
 */
public class AnimationUtil {
    private static final int DURATION = 2000;

    static {
    }

    /**
     * 立体式旋转(Y轴)
     */
    public static void rotate(View imageView) {
        TranslateAnimation ta = new TranslateAnimation(0, 0, 150, 0);//移动
        AlphaAnimation aa = new AlphaAnimation(0, 1);//透明度
        ScaleAnimation sa = new ScaleAnimation(1, 2, 1, 2);//缩放
        RotateAnimation ra = new RotateAnimation(0, 0, 0, 0);//旋转

        ObjectAnimator mObjectAnimator = new ObjectAnimator();
        mObjectAnimator.setDuration(DURATION);
        mObjectAnimator.setTarget(imageView);
        mObjectAnimator.setPropertyName("rotationY");
        mObjectAnimator.setFloatValues(0.0f, 180.0f);
        mObjectAnimator.setRepeatCount(-1);

        if (mObjectAnimator.isRunning()) {
            mObjectAnimator.cancel();
            return;
        }
        float value = (float) mObjectAnimator.getAnimatedValue();//to rotate
        mObjectAnimator.setFloatValues(value, value + 180);
        mObjectAnimator.start();

        Log.i("value", value + "");
    }

    /**
     * 立体式旋转(XY)
     *
     * @param imageView
     */
    public static void rotateXY(View imageView) {
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView,
                "rotationX",
                0.0f, 180.0f * 2);

        ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView,
                "rotationY",
                0.0f, 180.0f * 2);

        AnimatorSet as = new AnimatorSet();
        as.play(animator1).with(animator2);
        as.setDuration(DURATION);
        as.start();
    }

    public static void rotateX(View view, float from, float to) {
        ObjectAnimator animator = ObjectAnimator.ofFloat(view,
                "rotationX",
                from, to);
        animator.setDuration(DURATION);
        animator.start();
    }

    /**
     * 自定义包装类,改变view宽高
     */
    public static void scale(View imageView) {
        ObjectAnimator animator1 = ObjectAnimator.ofInt(new WarpView(imageView)
                , "width", imageView.getWidth(), imageView.getWidth() / 2);

        ObjectAnimator animator2 = ObjectAnimator.ofInt(new WarpView(imageView)
                , "height", imageView.getHeight(), imageView.getHeight() / 2);

//        ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "scaleX",
//                1.0f, 2.0f);
//        ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView, "scaleY",
//                1.0f, 2.0f);

        AnimatorSet animSet = new AnimatorSet();
        animSet.play(animator1).with(animator2);
        animSet.setDuration(2000);
        animSet.start();
    }

    /**
     * 改变view属性
     */
    public static void setTargetProperty(final View imageView) {
        final int startW = imageView.getWidth();
        final int startH = imageView.getHeight();

        ValueAnimator mValueAnimator = new ValueAnimator();
//        mValueAnimator = ValueAnimator.ofFloat(0.0f,1.0f);
        mValueAnimator.setFloatValues(1.0f, 2.0f);
        mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                Log.i("value", "value: " + value);
                RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
                layoutParams.width = (int) (startW * value);
                layoutParams.height = (int) (startH * value);
                imageView.setLayoutParams(layoutParams);
//                imageView.requestLayout();
            }
        });
        mValueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                Log.i("value", "startW: " + imageView.getWidth());
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                Log.i("value", "endW: " + imageView.getWidth());
            }
        });
        mValueAnimator.start();
    }

    /**
     * 移动到底部,并且alpha从1-0
     */
    public static void moveAlphaToBottom(View mImage_view) {
        ObjectAnimator animator0 = ObjectAnimator.ofFloat(mImage_view, "y", 0.0f, 180f);
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImage_view, "alpha", 1, 0);

        AnimatorSet as = new AnimatorSet();
        as.play(animator0).with(animator1);
        as.setDuration(DURATION);
        as.start();
    }

    /**
     * 自由落体
     */
    public static void g(final View mImage_view) {
        final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 180.0f);
//        animator.setTarget(mImage_view);
        animator.setInterpolator(new BounceInterpolator());
        animator.setDuration(DURATION);
        animator.start();

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float off = (float) animation.getAnimatedValue();
                mImage_view.setTranslationY(off);
            }
        });
    }

    public static void alpha(View view, float from, float to) {
        ObjectAnimator mObjectAnimator = new ObjectAnimator();
        mObjectAnimator.setTarget(view);
        mObjectAnimator.setPropertyName("alpha");
        mObjectAnimator.setFloatValues(from, to);
        mObjectAnimator.setDuration(DURATION);
        mObjectAnimator.start();
    }

    public static void y(View imageView) {
        ObjectAnimator mObjectAnimator = new ObjectAnimator();
        mObjectAnimator.setDuration(DURATION);
        mObjectAnimator.setTarget(imageView);
        mObjectAnimator.setPropertyName("y");
        mObjectAnimator.setFloatValues(0.0f, 180.0f);
        mObjectAnimator.start();
    }

    //相对原位置进行Values[1]偏移，从Values[0] - Values[1] target先瞬间到Values[0]位置,在偏移Values[1]
    public static void translationY(View imageView) {//偏移,从当前位置在向下偏移Y
        ObjectAnimator mObjectAnimator = new ObjectAnimator();
        mObjectAnimator.setDuration(DURATION);
        mObjectAnimator.setTarget(imageView);
        mObjectAnimator.setPropertyName("translationY");
        mObjectAnimator.setFloatValues(180.0f);
        mObjectAnimator.start();
    }

    /**
     * 卡牌文本介绍关闭效果：旋转角度与打开时逆行即可
     */
    public static void initCloseAnim(final int centerX, final int centerY, final int depthZ, final View view) {
        Rotate3dAnimation closeAnimation = new Rotate3dAnimation(360, 270, centerX, centerY, depthZ, true);
        closeAnimation.setDuration(DURATION);
        closeAnimation.setFillAfter(true);
        closeAnimation.setInterpolator(new AccelerateInterpolator());
        closeAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(90, 0, centerX, centerY, depthZ, false);
                rotateAnimation.setDuration(DURATION);
                rotateAnimation.setFillAfter(true);
                rotateAnimation.setInterpolator(new DecelerateInterpolator());
                view.startAnimation(rotateAnimation);
            }
        });

        view.startAnimation(closeAnimation);
    }

    /**
     * 卡牌文本介绍打开效果：注意旋转角度
     */
    public static void initOpenAnim(final int centerX, final int centerY, final int depthZ, final View view) {
        //从0到90度，顺时针旋转视图，此时reverse参数为true，达到90度时动画结束时视图变得不可见，
        Rotate3dAnimation openAnimation = new Rotate3dAnimation(0, 90, centerX, centerY, depthZ, true);
        openAnimation.setDuration(DURATION);
        openAnimation.setFillAfter(true);
        openAnimation.setInterpolator(new AccelerateInterpolator());
        openAnimation.setAnimationListener(new Animation.AnimationListener() {

            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                //从270到360度，顺时针旋转视图，此时reverse参数为false，达到360度动画结束时视图变得可见
                Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(270, 360, centerX, centerY, depthZ, false);
                rotateAnimation.setDuration(DURATION);
                rotateAnimation.setFillAfter(true);
                rotateAnimation.setInterpolator(new DecelerateInterpolator());
                view.startAnimation(rotateAnimation);
            }
        });

        view.startAnimation(openAnimation);
    }

    public static void keyFrameScaleXY(View view) {
        Keyframe k1 = Keyframe.ofFloat(0f, 1);
        Keyframe k2 = Keyframe.ofFloat(1f, 2);
        PropertyValuesHolder holder1 = PropertyValuesHolder.ofKeyframe("scaleX", k1, k2);
        PropertyValuesHolder holder2 = PropertyValuesHolder.ofKeyframe("scaleY", k1, k2);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, holder1, holder2);
        animator.setDuration(2000);
        animator.start();
    }

    //keyframe关键帧 存储动画执行进度和动画属性值（时间/值）对
    //用来定义动画的特定时间的帧。另外，每个关键帧可以拥有自己的插值器，用来控制动画在上一个关键帧和该关键帧的时间之间的间隔中的行为
    public static void keyFrameWidth(View view) {
        //开始时 Width=400
        //动画开始1/4时 Width=200
        //动画开始1/2时 Width=400
        //动画开始3/4时 Width=100
        //动画结束时 Width=500
        //第一个参数为时间百分比，第二个参数是在第一个参数的时间时的属性值。
        Keyframe kf0 = Keyframe.ofInt(0, 400);
        Keyframe kf1 = Keyframe.ofInt(0.25f, 200);
        Keyframe kf2 = Keyframe.ofInt(0.5f, 400);
        Keyframe kf4 = Keyframe.ofInt(0.75f, 100);
        Keyframe kf3 = Keyframe.ofInt(1f, 500);
        PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("width", kf0, kf1, kf2, kf4, kf3);
        ObjectAnimator animator = ObjectAnimator.ofPropertyValuesHolder(view, pvhRotation);
        animator.setDuration(2000);
        animator.start();

        //类似如下效果
//        ObjectAnimator oa=ObjectAnimator.ofInt(view, "width", 400,200,400,100,500);
//        oa.setDuration(2000);
//        oa.start();
    }

}
